﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using SARS.Biz;
using SARS.Biz.Facades;
using SARS.Entity;
using System.Web.UI.WebControls;

namespace SARS.Tutor
{
    public partial class StudentAbsentReasons : Page
    {
        private readonly IUserBO userBiz = new UserBO();
        private readonly IStudentAbsentReasonBO absentReason = new StudentAbsentReasonBO();

        private UserEntity user = null;
        private List<StudentAbsenceReasonEntity> listReason = new List<StudentAbsenceReasonEntity>();
        private int lastCourseId = 0;
        private int lastProgramId = 0;

        protected void Page_Load(object sender, EventArgs e)
        {

            Response.Cache.SetNoStore();
            if (Session["Roles"] == null || RoleType.Tutor != (RoleType)Session["Roles"])
            {
                Response.Write("<script language='javascript'>window.alert('Your role is not allow to access this page.');" +
                               "window.location='../Account/Login.aspx';</script>");

            }
            else
            {
                user = userBiz.RetrieveUserByUserName(Page.User.Identity.Name);
                listReason = absentReason.GetAllStudentAbsenceReasonByTutorId(user.UserID);    
            }
            
            
        }

        protected void ucProgramCourse_OnSelectedCourseChanged()
        {
            if (ucProgramCourse.SelectedCourseValue != 0)
            {
                lastCourseId = ucProgramCourse.SelectedCourseValue;
                var listByCourse = listReason.FindAll(i => i.AttendanceSession.Session.CourseID == lastCourseId);

                gvAttendanceAbsence.DataSource = listByCourse;
                gvAttendanceAbsence.DataBind();
            }
        }

        protected void btnSelect_OnClick(object sender, EventArgs e)
        {
            var btnSelect = sender as LinkButton;
            if (btnSelect != null)
            {
                var row = (GridViewRow)btnSelect.NamingContainer;
                string attendanceSession = row.Cells[0].Text;
                var att = listReason.Find(i => i.AttendanceSessionId == Int32.Parse(attendanceSession));
                string linkurl = string.Format("ReviewAbsentRequest.aspx?atsesid={0}&sesid={1}&coid={2}&proid={3}&stuid={4}"
                                               , attendanceSession
                                               ,att.AttendanceSession.SessionID
                                               ,ucProgramCourse.SelectedCourseValue
                                               ,ucProgramCourse.SelectedProgramValue
                                               ,att.AttendanceSession.StudentID);

                Response.Redirect(linkurl);
            }

        }

        protected void ucProgramCourse_OnSelectedProgramChanged()
        {
            if (ucProgramCourse.SelectedProgramValue != 0)
            {
                lastProgramId = ucProgramCourse.SelectedProgramValue;
            }
        }
    }
}